package com.aaa.doctor.controller.api;

import com.aaa.doctor.service.PatientService;
import com.aaa.doctor.vo.Dvo;
import com.aaa.doctor.vo.PatientVo;
import com.aaa.entity.Patient;
import com.aaa.entity.Registration;
import com.aaa.vo.Result;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/doctor/api/patient")
public class ApiPatientController {

    @Autowired
    private PatientService patientService;

    /**
     * 患者库--查询所有患者
     * @param currentPage
     * @param pageSize
     * @param patientVo
     * @return
     */
    @PostMapping("/getAllPatient/{currentPage}/{pageSize}")
    public Result<IPage<Patient>> getAllPatient(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @RequestBody PatientVo patientVo){
        return patientService.getAllPatient(currentPage,pageSize,patientVo);
    }

    /**
     * 根据patientId查询档案信息 -- 两表联查
     * @param patientId
     * @return
     */
    @GetMapping("/getInfoByPatientId/{patientId}")
    public Result<Patient> getInfoByPatientId(@PathVariable String patientId){
        return patientService.getInfoByPatientId(patientId);
    }

    /**
     * 根据身份证号加载患者信息
     * @param idCard
     * @return
     */
    @PostMapping("/getInfoByidCard/{idCard}")
    public Result<Patient> getInfoByidCard(@PathVariable String idCard){
        return patientService.getInfoByidCard(idCard);
    }

    //

    /**
     * 根据患者ID查询患者信息、档案信息、病历信息
     * 三表联查 his_patient his_patient_file his_care_history
     * @param patientId
     * @return
     */
    @GetMapping("/getAllByPatientId/{patientId}")
    public Result<Map<String,Object>> getAllByPatientId(@PathVariable String patientId){
        return patientService.getAllByPatientId(patientId);
    }


    /**
     *  给患者挂号
     * @param map
     * @return
     */
    @PostMapping("/addRegistration")
    public Result <String> addRegistration(@RequestBody Map<String,Object> map){

        Patient patient1 = JSON.parseObject(JSON.toJSONString(map.get("a1")), Patient.class);
        // Object里 LinkListMap里面不支持Map转对象，需要先转成字符串对象，然后再将字符串转换成指定对象

        Registration patient2 = JSON.parseObject(JSON.toJSONString(map.get("a2")), Registration.class);

        Dvo a3 = JSON.parseObject(JSON.toJSONString(map.get("a3")), Dvo.class);
        return patientService.addRegistration(patient1,patient2,a3);

    }

}
